package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Date;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;

public class CreateSet extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      request.setCharacterEncoding("UTF-8");
      PrintWriter outp = response.getWriter();
      outp.println("<HTML>");
      outp.println("<BODY>");
      try {
         MongoClient mongo = new MongoClient("localhost", 27017); 
         DB db = mongo.getDB("settingdb");
         DBObject options = BasicDBObjectBuilder.start().add("capped", false).add("size", 2000000000l).get();
         DBCollection table;
         if(!db.collectionExists("setlog")) {
            table = db.createCollection("setlog",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("unique", true));
         }
         if(!db.collectionExists("adrule")) {
            table = db.createCollection("adrule",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("adrule",1).append("unique", true));
         }
         if(!db.collectionExists("adid")) {
            table = db.createCollection("adid",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("adid",1).append("unique", true));
         }
         if(!db.collectionExists("top")) {
            table = db.createCollection("top",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("server",1).append("unique", true));
         }
         if(!db.collectionExists("action")) {
            table = db.createCollection("action",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("name",1).append("unique", true));
         }
         if(!db.collectionExists("adana")) {
            table = db.createCollection("adana",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("id",1).append("unique", true));
         }
         if(!db.collectionExists("adanarule")) {
            table = db.createCollection("adanarule",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("rule",1).append("unique", true));
         }
         if(!db.collectionExists("default")) {
            table = db.createCollection("default",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("name",1).append("unique", true));
         }
         if(!db.collectionExists("groupad")) {
            table = db.createCollection("groupad",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("name",1).append("unique", true));
         }
         if(!db.collectionExists("grouptype")) {
            table = db.createCollection("grouptype",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("server",1).append("show_mode",1).append("groupno",1).append("unique", true));
         }
         if(!db.collectionExists("impmsgad")) {
            table = db.createCollection("impmsgad",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("name",1).append("unique", true));
         }
         if(!db.collectionExists("policy")) {
            table = db.createCollection("policy",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("type",1).append("unique", true));
         }
         if(!db.collectionExists("round")) {
            table = db.createCollection("round",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("name",1).append("unique", true));
         }
         if(!db.collectionExists("member")) {
            table = db.createCollection("member",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("member",1).append("unique", true));
         }
         if(!db.collectionExists("server")) {
            table = db.createCollection("server",options);
            table.ensureIndex(new BasicDBObject("start", 1).append("server",1).append("show_mode",1).append("unique", true));
         }
         if(!db.collectionExists("control")) {
            table = db.createCollection("control",options);
            table.ensureIndex(new BasicDBObject("user", 1).append("unique", true));
         }
       } catch (Exception ex) {
          outp.println("mongodb exception:"+ex.getMessage());
       }
       outp.println("create collections done");
       outp.println("</HTML>");
       outp.println("</BODY>");

   }
}
